﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App.Core.Extensions;
using App.Data;
using System.Data;

public partial class Pages_Private_Admin_Packages : BasePage
{
    private int _ID = 0;
    private bool _IsEditMode = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {
            BindDropDownLists();
            BindPackageInfo();
            BindPackageList();
        }
    }
    /// <summary>
    /// Bindis the Page Initialization Variables
    /// </summary>
    protected void BindPageInfo()
    {
        _ID = WebUtil.GetQueryStringInInt(AppConstants.QueryString.ID);
        if (_ID > 0)
        {
            _IsEditMode = true;
            //h4Heading.InnerHtml = "Edit Package";
        }
        Page.Title = WebUtil.GetPageTitle(h4Heading.InnerHtml);
    }

    /// <summary>
    /// Binds Dropdownlists for the initial request.
    /// </summary>
    protected void BindDropDownLists()
    {
        BindDropdownList.Currencies(ddlCurrencyID);
    }
    /// <summary>
    /// Binds Package Info Requested through Query Strings
    /// </summary>
    protected void BindPackageInfo()
    {
        if (_IsEditMode)
        {
            BtcHousingEntities context = new BtcHousingEntities();
            Package entity = context.Packages.FirstOrDefault(P => P.ID == _ID);//dao.GetByID(_ID);
            if (entity == null)
                ShowErrorMessage();
            else
            {
                txtName.Text = entity.Name;
                txtPrice.Text = entity.Price.ToString();
                ddlCurrencyID.SetSelectedItem(entity.CurrencyID.ToString());
                txtDuration.Text = entity.Duration.ToString();
                txtProductLimit.Text = entity.ProductLimit.ToString();
                chkIsActive.Checked = entity.IsActive;
            }
        }
    }
    protected void BindPackageList()
    {
        UtilityDAO dao = new UtilityDAO();
        DataSet ds = dao.GetDataSet(AppSQL.GET_PACKAGE_LIST, null, false);
        ucPackageList.DataSource = ds.Tables[0];
        ucPackageList.EditLink = Request.Url.AbsolutePath + "?" + AppConstants.QueryString.ID + "={0}";
        ucPackageList.DeleteLink = Request.Url.AbsolutePath + "?" + AppConstants.QueryString.ID + "={0}&" + AppConstants.QueryString.DELETE + "=True";
        ucPackageList.DataBind();
    }
    /// <summary>
    /// Shows a Message in the UI and Hides the Data Editing Controls
    /// </summary>
    protected void ShowErrorMessage()
    {
        pnlFormContainer.Visible = false;
        WebUtil.ShowMessageBox(divMessage, "Requested Package was not found.", true);
    }
    protected void SavePackage()
    {
        BtcHousingEntities context = new BtcHousingEntities();
        Package entity = null;

        if (_IsEditMode)
            entity = context.Packages.FirstOrDefault(P => P.ID == _ID); //dao.GetByID(_ID);        
        else
        {
            entity = new Package();
            entity.Created = DateTime.Now;
            context.AddToPackages(entity);
        }       
        entity.Name = txtName.Text;
        entity.Price = txtPrice.Text.ToDecimal();
        entity.CurrencyID = ddlCurrencyID.SelectedValue.ToInt();
        entity.Duration = txtDuration.Text.ToInt();
        entity.ProductLimit = txtProductLimit.Text.ToInt();
        entity.IsActive = chkIsActive.Checked;
        entity.Modified = DateTime.Now;

        context.SaveChanges();
        WebUtil.ShowMessageBox(divMessage, "Package Saved Successfully.", false);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            SavePackage();            
            Response.Redirect(Request.Url.AbsolutePath);
            return;
        }
    }
    //protected void btnList_Click(object sender, EventArgs e)
    //{
    //    Response.Redirect(AppConstants.Pages.PACKAGE_LIST);
    //    return;
    //}
}